package com.envmonitor.domain.service;

import com.envmonitor.domain.model.EnvironmentData;
import com.envmonitor.domain.repository.EnvironmentDataRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class EnvironmentDataService {
    
    private final EnvironmentDataRepository environmentDataRepository;
    
    public List<EnvironmentData> getAllData() {
        return environmentDataRepository.findAll();
    }
    
    public Optional<EnvironmentData> getDataById(Long id) {
        return environmentDataRepository.findById(id);
    }
    
    public List<EnvironmentData> getDataByDeviceId(Long deviceId) {
        return environmentDataRepository.findByDeviceId(deviceId);
    }
    
    public List<EnvironmentData> getDataByTimeRange(LocalDateTime start, LocalDateTime end) {
        return environmentDataRepository.findByRecordedAtBetween(start, end);
    }
    
    @Transactional
    public EnvironmentData saveData(EnvironmentData data) {
        return environmentDataRepository.save(data);
    }
    
    @Transactional
    public void deleteData(Long id) {
        environmentDataRepository.deleteById(id);
    }
    
    public List<EnvironmentData> getDataByTemperatureThreshold(Double threshold) {
        return environmentDataRepository.findByTemperatureGreaterThan(threshold);
    }
    
    public List<EnvironmentData> getDataByHumidityThreshold(Double threshold) {
        return environmentDataRepository.findByHumidityGreaterThan(threshold);
    }
    
    public List<EnvironmentData> getDataByEnergyConsumptionThreshold(Double threshold) {
        return environmentDataRepository.findByEnergyConsumptionGreaterThan(threshold);
    }
}